<?php

class Forms_User extends Zend_Form {

    private $decoratorUser = array();

    // adds the <div to the first button
    public $openButtonDecorators = array(
        'ViewHelper',
        array('HtmlTag', array('tag' => 'div', 'class' => 'element', 'openOnly'=>true))
    );

    // just outputs a button no decorator
    public $noButtonDecorators = array(
       'ViewHelper',
    );

    //closes the div on the last button.
    public $closeButtonDecorators = array(
        'ViewHelper',
        array('HtmlTag', array('tag' => 'div', 'class' => 'element', 'closeOnly'=>true))
    );
    
    public function init() {
        /*
         * Set decorator for the form
         */
        $this->decoratorUser = array(
            'ViewHelper',
            array('ViewScript', array('viewScript' => '/decorators/decoratorUser.phtml', 'placement' => false)),
        );
    }

    public function deleteUser($datos = array())
    {
        $form = new Zend_Form();
        
        $form->setName('addUser')
             ->setAction('/admin/user/delete/' . $datos['id'])
             ->setMethod('post');
        
        $form->addElement('hidden', 'id', array('value' => $datos['id']));
        
        $submitButton = new Zend_Form_Element_Button('submit');
        $submitButton->setRequired(false)
            ->setLabel('Submit')
            ->setValue('submit')
            ->setDecorators($this->openButtonDecorators)
            ->setAttribs(array('type' => 'submit'));
        $form->addElement($submitButton);

        $cancelButton = new Zend_Form_Element_Button('cancel');
        $cancelButton->setRequired(false)
            ->setLabel('Cancel')
            ->setValue('cancel')
            ->setDecorators($this->closeButtonDecorators)
            ->setAttribs(array('type' => 'submit'));
        $form->addElement($cancelButton);
        
        return $form;

    }
    
    public function addUser($datos = array()) {
        /*
         * Create a validation if input is empty
         */
        $notEmpty = new Zend_Validate_NotEmpty();

        /*
         * Create a new form
         */
        $form = new Zend_Form();
        
        if (!$datos['id']) {
            $form->setName('addUser')
                    ->setAction('/admin/user/add')
                    ->setMethod('post');
        } else {
            $form->setName('addUser')
                    ->setAction('/admin/user/edit/' . $datos['id'])
                    ->setMethod('post');
        }
        /*
         * Add hidden form with id of user, this is if user are editing
         */
        if ($datos['id']) {
            $form->addElement('hidden', 'id', array('value' => $datos['id']));
        }

        /*
         * User name input
         */
        $user = $form->createElement("text", "formuser")
                        ->setLabel("User Name:")
                        ->setRequired(true)
                        ->addValidator($notEmpty, true)
                        
                        ->setAttrib("class", "textfield")
                        ->setValue($datos['user'])
                        ->addValidator(
                                'regex',
                                true,
                                array(
                                    '/^[\w-]+$/', // regex test
                                )
                        )
                       ->addValidator( 'Alpha',
                array('StringLength', true, array(3, 20)))
                        ->setDecorators($this->decoratorUser);

        $form->addElement($user);

        /*
         * Role select
         */
        $role = new Core_Role();
        $rolesArray = $role->getAll();
        $arrayRoles = array();
        foreach ($rolesArray as $role) {
            $arrayRoles[$role['id']] = $role['role'];
        }
        $role = $form->createElement("select", "formrole")
                        ->setLabel("Role:")
                        ->setAttrib("class", "dropdown")
                        ->setDecorators($this->decoratorUser)
                        ->setValue($datos['id_role'])
                        ->setMultiOptions($arrayRoles)
                        ->setRequired(true, true)
                        ->addValidator($notEmpty);

        $form->addElement($role);
        
        /*
         * Password input
         */
        $password = $form->createElement("password", "formpassword")
                        ->setLabel("Password:")
                        ->setAttrib("class", "textfield")
                        ->setDecorators($this->decoratorUser);

        /*
         * For edit, don't need password
         */
        if (!$datos['id']) {
            $password->setRequired(true)
                    ->addValidator($notEmpty, true);
        }

        $form->addElement($password);

        /*
         * email input
         */
        $email = $form->createElement("text", "formemail")
                        ->setLabel("Email:")
                        ->setAttrib("class", "textfield")
                        ->setRequired(true)
                        ->addValidator($notEmpty, true)
                        ->setValue($datos['email'])
                        ->addValidator('regex', true,
                                array('/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/', // regex test
                                    'messages' => array(// error messages
                                        Zend_Validate_Regex::INVALID => 'Error mail format (Cambiar mensaje)',
                                        Zend_Validate_Regex::NOT_MATCH => 'Error mail format (Cambiar mensaje)'
                                    )
                                )
                        )
                        ->setDecorators($this->decoratorUser);
        $form->addElement($email);

        /*
         * Submit button
         */
        if (!$datos['id']) {
            $form->addElement('submit', 'addUserButton', array(
                'ignore' => true,
                'label' => 'Crear',
            ));
        } else {
            $form->addElement('submit', 'addUserButton', array(
                'ignore' => true,
                'label' => 'Guardar',
            ));
        }

        return $form;
    }

}
